package model;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;

public class Exchange {

	//替换implements Serializable
	//去掉一些方法
	private static void createModel(File dir) {
		File[] files = dir.listFiles(new FilenameFilter() {

			public boolean accept(File dir, String name) {
				if (name.endsWith(".java")) {
					return true;
				}
				return false;
			}
		});

		for (File file : files) {
			try {
				changeFile(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static void changeFile(File file) throws IOException {

		if (file.isFile()) {
			FileReader fr = new FileReader(file);

			char[] cbuf = new char[(int) file.length()];
			int len = fr.read(cbuf);
			String ori = new String(cbuf, 0, len);
			String ret = ori.replace("implements Serializable", "extends AbstractAuditable<Long>");
			ret = ret.replace("@Id", "");
			ret = ret.replace("private String id;", "");

			fr.close();
			FileWriter fw = new FileWriter(new File(file.getAbsolutePath() + ".bak"));
			fw.write(ret);
			fw.close();
		}
	}

	/*	
		package org.macula.samples.demo.repository;

		import org.macula.core.repository.MaculaJpaRepository;
		import org.macula.samples.demo.domain.LeaveRequest;
	*/
	/**
	 * <p>
	 * <b>LeaveRequestRepository</b> is
	 * </p>
	 *
	 * @since 2013-7-5
	 * @author jian_yang
	 * @version $Id: codetemplates.xml 4147 2013-7-5 上午10:28:56 young $
	 */
	/*	public interface LeaveRequestRepository extends MaculaJpaRepository<LeaveRequest, Long> {

		}
		*/
	private static void createRepos(File dir) {
		File[] files = dir.listFiles(new FilenameFilter() {

			public boolean accept(File dir, String name) {
				if (name.endsWith(".java")) {
					return true;
				}
				return false;
			}
		});

		for (File file : files) {
			try {
				writeRepo(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private static void writeRepo(File file) throws IOException {
		if (file.isFile()) {
			String path = file.getAbsolutePath();
			int offset = path.lastIndexOf(File.separator);
			path=path.substring(0, offset+1);
			String name = file.getName();
			offset = name.indexOf(".");
			String className = name.substring(0,offset);
//			System.out.println(path);
//			System.out.println(className);
			String repoName = className+"Repository";
			String ret = "package com.skywin.otms.repository;";
			ret+="\r\n";
			ret+= "import org.macula.core.repository.MaculaJpaRepository;";
			ret+="\r\n";
			ret+= "import com.skywin.otms.model."+className+";";
			ret+="\r\n";
			ret+="\r\n";
			ret+="public interface "+repoName+" extends MaculaJpaRepository<"+className+", Long> {";
			ret+="\r\n";
			ret+="}";
			ret+="\r\n";
			FileWriter fw = new FileWriter(new File("F:/otms/genjava/src/"+repoName+".java"));
			fw.write(ret);
			fw.close();
			//System.out.println(ret);
		}
	}

	public static void main(String[] args) {
		File dir = new File("F:/otms/otms/otms-dao/src/main/java/com/skywin/otms/model");
		//createModel(dir);
		createRepos(dir);
	}

}
